/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.hobbiestag;

import com.apt.bean.Hobbies;
import com.apt.bean.Liking;
import com.apt.hobbiestag.process.GetListLiking;
import com.apt.hobbiestag.process.Process_PageHobbies;
import java.util.HashMap;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class LikingTagHandler extends SimpleTagSupport {

    private String page;
    private String modlename;
    private String liking_Model_Id;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;

        HashMap<Integer, Hobbies> hm = GetListLiking.getListHobbies(liking_Model_Id);
        try {
            if (hm != null && hm.size() != 0) {
                out.println(Process_PageHobbies.divPage(page, hm.size(), 16, modlename, liking_Model_Id));
                int uid_f = 1;
                int uid_l = 30;

                try {
                    cursor1 = Integer.parseInt(page);

                } catch (NumberFormatException ex) {
                    cursor1 = 1;
                }
                if (cursor1 > 1) {
                    uid_f = (cursor1 - 1) * 30;
                    uid_l = cursor1 * 30;
                }
                int current_page = uid_l / 30;
                for (int i = uid_f; i < uid_l; i++) {
                    Hobbies u = hm.get(i);
                    if (u != null) {
                        int status = Integer.parseInt(u.getLiking_Model_Status());
                        String st;
                        String liking_Id = u.getLiking_Id();
                        String liking_Modle_Id = u.getLiking_Model_Id();
                        String liking_Name = u.getLiking_Model_Name();
                        if (status == 1) {
                            st = "Enable";
                        } else {
                            st = "Disable";
                        }
                        StringBuilder sb = new StringBuilder();
                        if (i == 1) {

                            sb.append("  <tr>"
                                    + "<form action=\"UpdateLikingServlet\" method=\"get\" name=\"frm" + liking_Id + "\">"
                                    + "<td>" + modlename + "</td>\n");

                            if (status == 1) {
                                sb.append(" <td><input type=\"text\" name=\"hobbiename\" value=\"" + liking_Name + "\"</td> \n");// name
                                sb.append("<td>" + st + "</td>\n");
                                sb.append("<td><a href=\"DisableLikingServlet?liking_Id=" + liking_Id + "&pageno=" + current_page + "&liking_Model_Name="
                                        + modlename + "&liking_Model_Id=" + liking_Modle_Id + "\" class=\"button\" >Disable</a>");

                                sb.append("<input type=\"hidden\" name=\"liking_Model_Name\" value=\"" + modlename + "\"/>");
                                sb.append("<input type=\"hidden\" name=\"liking_Model_Id\" value=\"" + u.getLiking_Model_Id() + "\"/>");
//lay id,name
//                                liking_Model_Name
//                                liking_Model_Id
                                sb.append("<input type=\"hidden\" name=\"l_id\" value=\"" + liking_Id + "\"/>");//id

                                sb.append(" <span>----</span> <input  class=\"button\" type=\"submit\"  value=\"Update\"/></td>");

                            } else {
                                sb.append(" <td>" + liking_Name + "</td> \n");
                                sb.append("<td>" + st + "</td>\n");
                                sb.append("<td><a href=\"EnableLikingServlet?liking_Id=" + liking_Id + "&pageno=" + current_page + "&liking_Model_Name="
                                        + modlename + "&liking_Model_Id=" + liking_Modle_Id + "\" class=\"button\">Enable</a></td>\n");
                            }
                            sb.append("</form>\n" + "</tr>\n");

                        } else {//khu nay lam truoc
                            sb.append("  <tr><td>___________________ "
                                    + "<form action=\"UpdateLikingServlet\" method=\"get\" name=\"frm" + liking_Id + "\">  </td>\n");
                            if (status == 1) {
                                sb.append(" <td><input type=\"text\" name=\"hobbiename\" value=\"" + liking_Name + "\"</td> \n");// name
                                sb.append("<td>" + st + "</td>\n");
                                sb.append("<td><a href=\"DisableLikingServlet?liking_Id=" + liking_Id + "&pageno=" + current_page
                                        + "&liking_Model_Name=" + modlename
                                        + "&liking_Model_Id=" + liking_Modle_Id + "\" class=\"button\" >Disable</a>");

                                sb.append("<input type=\"hidden\" name=\"liking_Model_Name\" value=\"" + modlename + "\"/>");
                                sb.append("<input type=\"hidden\" name=\"liking_Model_Id\" value=\"" + u.getLiking_Model_Id() + "\"/>");
//lay id,name
//                                liking_Model_Name
//                                liking_Model_Id
                                sb.append("<input type=\"hidden\" name=\"l_id\" value=\"" + liking_Id + "\"/>");//id

                                sb.append(" <span>----</span> <input  class=\"button\" type=\"submit\"  value=\"Update\"/></td>");
                                //den day
                            } else {
                                sb.append(" <td>" + liking_Name + "</td> \n");
                                sb.append("<td>" + st + "</td>\n");
                                sb.append("<td><a href=\"EnableLikingServlet?liking_Id=" + liking_Id + "&pageno=" + current_page + "&liking_Model_Name="
                                        + modlename + "&liking_Model_Id=" + liking_Modle_Id + "\" class=\"button\" >Enable</a></td>\n");
                            }
                            sb.append("</form>\n" + "</tr>\n");

                        }
                        out.print(sb.toString());

                    }
                }
            }
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ProfessionnalTagHandler tag", ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }

    /**
     * @return the modlename
     */
    public String getModlename() {
        return modlename;
    }

    /**
     * @param modlename the modlename to set
     */
    public void setModlename(String modlename) {
        this.modlename = modlename;
    }

    /**
     * @return the liking_Model_Id
     */
    public String getLiking_Model_Id() {
        return liking_Model_Id;
    }

    /**
     * @param liking_Model_Id the liking_Model_Id to set
     */
    public void setLiking_Model_Id(String liking_Model_Id) {
        this.liking_Model_Id = liking_Model_Id;
    }
}
